<template lang="pug">
	div
		.title {{ item.title }}
		p.small(v-if="item.contents") {{ item.author }} {{ item.timeCreate | time }}
		div(v-for="i in item.contents" style="margin:0 0;padding:0 0;")
			div(v-if="i.type===8") 
				img.media-object(:src="'https://qiniu443.alltennis.cn/'+i.data" v-on:click="jumpto(i.url)" style="width:100%;")
			div(v-if="i.type===1" v-html="i.data")
		p#qr(v-if="item.contents")
			img(src="~../qrcode.png")
</template>
<style scoped>
	.title{
		font-size: 24px;
	}
	.meta{
		font-size: 16px;
	}
	#qr{
		text-align: center;
	}
	#qr>img{
		margin: auto;
		width : 75%;
	}
</style>
<script>
	const preFetch = function (route, store) {
		route  = route || this.$route
		store  = store || this.$store

		return store.dispatch('getNews', route.params.id)
	}

	const appendScript = function (src) {
		const s = document.createElement('script')
		s.src = src

		document.getElementsByTagName('body')[0].appendChild(s)
	}

	module.exports = {
		computed: {
			item () {
				return this.$store.state.news || {}
			},
		},
		preFetch: preFetch,
		mounted: function () {
			preFetch.call(this)

			appendScript(`http://139.196.206.146:9999/api/2/topic/click?id=${this.$route.params.id}`)

			const url = {
				'584f8888fa51544726016a91': 'http://139.196.206.146:9998/api/2/ads/96022bb35d0b9188/identify',
				'584f6955fa51547b600b4201': 'http://139.196.206.146:9998/api/2/ads/dc58f8160fb6dbe5/identify',
				'584f5573fa515415c11020a3': 'http://139.196.206.146:9998/api/2/ads/3a4f2b88986583f6/identify',
				'584f3c55fa515415c1102089': 'http://139.196.206.146:9998/api/2/ads/00bf1853db0e2926/identify',
				'584f3425fa515415c1102083': 'http://139.196.206.146:9998/api/2/ads/63b9beaba155970d/identify',
			}[this.$route.params.id]

			if (url) {
				appendScript(url)
			}

		},
		watch: {
			'item.title': function (newV, oldV) {
				document.title = newV
			}
		},
                methods: {
                        jumpto (path) {
                                if (path) {
                        		const type = this.check_url_type(path)
					if(type==='app'){
						top.location = decodeURI(path)
					}else if(type==='url'){
                                 		top.location = decodeURI(path)
                                        }else{
						top.location = 'https://api2.alltennis.cn/wechat/#!'+decodeURI(path)
					}        	
                                } 
                        },
			check_url_type(url) {
    				//$Utils.notice('check_url_type: '+url)
    				const httpExp = '^(http|https|ftp)+://[^\s]*';
    				if (url.match(httpExp)) {
        				return 'url';
    				} else {
        				let urlExp = '[a-zA-z_-]://[^\s]*';
        				if (url.match(urlExp)) {
            					return 'app';
        				} else {
            					return 'route';
        				}
    				}
			}
                }
	}
</script>
